var vue = new Vue({
    el: "#main",
    data() {
        return {
            tagList: [],
            backTop: false,
            isShow: false,
            placeList: [],
            myPlaceList: [],
            id: '',
            cateIndex: -1,
            cateName: '',
            placeObj: {
                grids_id: '',
                cate_id: '',
                keywords: '',
                page: 1,
                location: '',
                is_small_place: '',
                is_cater: '',
                tags: ""
            },
            total: '',
            flag: false,
            gridList: [],
            type: '',
            showLoading: false,
            categoryTotal: '',
            isPlace9: false,
            isCatering: false,
            tags: [],
        }
    },
    mounted() {
        this.placeObj.location = localStorage.getItem("location")
        this.placeObj.grids_id = getRequest().id
        this.placeObj.cate_id = getRequest().cate_id
        this.placeObj.keywords = getRequest().keywords || ''
        this.type = getRequest().type
        this.getPlaceList()
        this.getMyPlaceList(getRequest().id)
        this.getGridList()
        this.getTags()
        // this.getScrollTop()
        window.addEventListener('scroll', this.onScroll, true)
        this.getTags()
    },
    methods: {
        // 获取场所
        getTags() {
            ajax({
                url: formal + '/place/tags',
                type: 'GET',
                success: (res) => {
                    this.tagList = res.data.list
                }
            })
        },
        // 点击返回顶部
        topclick() {
            var s = document.documentElement.scrollTop;
            // 定时器 每10ms执行一次
            timer = window.setInterval(function () {
                // 每次走50
                s -= 50;
                //  到顶部后清除定时器  必须清定时器  不然就死循环了
                if (s < 0) {
                    window.clearInterval(timer);
                }
                window.scrollTo(0, s);
            }, 10);


        },
        // 社区分类
        getGridList() {
            let obj = {}
            if (this.type == 1) {
                obj = {
                    cate_id: this.placeObj.cate_id
                }
            }
            if (this.type == 2) {
                obj = {
                    id: this.placeObj.id
                }
            }
            ajax({
                url: formal + '/place/get-place-community',
                type: 'GET',
                data: obj,
                success: (res) => {
                    let newItem = {}
                    res.data.list.map(item => {
                        newItem = item
                        newItem.flag = false
                    })
                    this.gridList = res.data.list
                }
            })
        },
        // 触底刷新列表
        onScroll(e) {
            this.showLoading = true
            if (this.flag === true) {
                this.showLoading = false
                return
            }
            //变量scrollTop是滚动条滚动时，距离顶部的距离
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            if (scrollTop > 200) {
                this.backTop = true
            } else {
                this.backTop = false
            }
            //变量windowHeight是可视区的高度
            var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
            //变量scrollHeight是滚动条的总高度
            var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
            //滚动条到底部的条件
            let page = 1
            if (scrollHeight - (scrollTop + windowHeight) < 100) {
                //写后台加载数据的函数
                page = ++this.placeObj.page
                this.getPlaceList(page)
                console.log("距顶部" + scrollTop + "可视区高度" + windowHeight + "滚动条总高度" + scrollHeight);
            }
        },
        // 获取行业筛选条件
        chooseIndustry(index, id) {
            this.cateIndex = index
            this.placeObj.cate_id = id
            if (index == 99) {
                this.placeObj.cate_id = ''
            }
        },
        // 获取社区筛选条件
        chooseIndustry1(index, id) {
            this.cateIndex = index
            this.placeObj.dept_id = id
            if (index == 99) {
                this.placeObj.dept_id = ''
            }
        },
        // 行业分类
        getMyPlaceList(id) {
            ajax({
                url: formal + '/place/category',
                type: 'GET',
                data: {
                    grids_id: id
                },
                success: (res) => {
                    let newItem = {}
                    this.myPlaceList = res.data.list
                    this.categoryTotal = res.data.total
                }
            })
        },
        // 显示筛选弹窗
        showModal() {
            this.isShow = true
        },
        // 场所列表
        getPlaceList(page) {
            this.showLoading = true
            if (page == undefined) {
                page = 1
            }
            if (page == 1) {
                this.flag = false
            }
            if (this.isPlace9) {
                this.placeObj.is_small_place = 1
            } else {
                this.placeObj.is_small_place = 0
            }
            if (this.isCatering) {
                this.placeObj.is_cater = 1
            } else {
                this.placeObj.is_cater = 0
            }
            this.placeObj.page = page
            this.placeObj.tags = this.tags.join(",")
            ajax({
                url: formal + '/place/get-grids-place',
                type: 'GET',
                data: this.placeObj,
                success: (res) => {
                    if (res.data.list.length == 0) {
                        this.showLoading = false
                        if (res.data.total == 0) {
                            this.placeList = []
                        }
                        this.total = res.data.total
                        this.flag = true
                    } else {
                        if (page > 1) {
                            console.log(111)
                            this.placeList = this.placeList.concat(res.data.list)
                            this.arrRemoval(this.placeList)
                            this.total = res.data.total
                        } else {
                            this.placeList = res.data.list
                            this.total = res.data.total
                        }
                    }
                    this.showLoading = false
                    this.isShow = false
                    this.cateName = res.data.cate_name
                },
                error: (res) => {
                    this.$message({
                        message: res.message,
                        type: 'error',
                        duration: 3000
                    });
                }
            })
        },
        // 数组去重
        arrRemoval(arr) {
            let obj = {}
            this.placeList = arr.reduce((preVal, curVal) => {
                obj[curVal.id] ? "" : obj[curVal.id] = preVal.push(curVal)
                return preVal
            }, [])
            console.log(this.sonPlaceList)

        },
        // 跳转到场所详情
        goPlaceDetail(id) {
            sessionStorage.setItem('cate_id', this.placeObj.cate_id)
            sessionStorage.setItem('type', this.type)
            window.location.href = "placeDetail.html?id=" + id
        },
    }
})